
package com.zsrt.day20170417.ch03;


public class PetDaoImpl implements PetDao{

	
	
	@Override
	public Pet[] updatePetsByConditions(PetShop huarun, Pet condition,Pet alterPets) {

		//先确定要删的个数
		Pet[] pets = huarun.getPets();
		String name= condition.getName();
		int age    = condition.getAge();
		String color=condition.getColor();
		
		
		int count=0;
		//假如条件里的名字为空
		if(name == null){
			for( Pet pet :pets){
				if(pet != null){
					count = pdByAgeAndColor(age, color, count, pet);
					System.out.println(count);
				}
			}
			
		}else{
			//名字不是null时
			for(Pet pet:pets){
				if(pet != null){
					String findName = pet.getName();
					if(findName.equals(name)){
						//名字相等时
						count = pdByAgeAndColor(age, color, count, pet);
						System.out.println(count);
					}
				}
			}
			
		}
		
		//得出要改变的是几个对象
		Pet[] newPets=new Pet[count];
		
		//进行改变
		count=0;
		
		//得出改变后的三个值
		@SuppressWarnings("unused")
		String alterName = alterPets.getName();
		int alterAge = alterPets.getAge();
		String alterColor = alterPets.getColor();
		
		//假如条件里的名字为空
		if(name == null){
			for( Pet pet :pets){
				if(pet != null){
					int findAge = pet.getAge();
					if(findAge==age){
						//年龄相等时 符合条件的年龄
						String findColor = pet.getColor();
						//当颜色也符合条件时
						if(color.equals(findColor)){
							//找到了
							pet.setAge(alterAge);
							pet.setColor(alterColor);
							
							newPets[count]=pet;
							++count;
						}
						
					}
					System.out.println(count);
				}
			}
			
		}else{
			//名字不是null时
			for(Pet pet:pets){
				if(pet != null){
					String findName = pet.getName();
					if(findName.equals(name)){
						//名字相等时
						int findAge = pet.getAge();
						if(findAge==age){
							//年龄相等时 符合条件的年龄
							String findColor = pet.getColor();
							//当颜色也符合条件时
							if(color.equals(findColor)){
								//找到了
								pet.setAge(alterAge);
								pet.setColor(alterColor);
								pet.setName(findName);
								newPets[count] = pet;
								++count;
							}
							
						}	
						System.out.println(count);
					}
				}
			}
			
		}
		
		return newPets;
	}
	
	
	private int pdByAgeAndColor(int age, String color, int count, Pet pet) {
		int findAge = pet.getAge();
		if(findAge==age){
			//年龄相等时 符合条件的年龄
			String findColor = pet.getColor();
			//当颜色也符合条件时
			if(color.equals(findColor)){
				//找到了
				
				count++;
			}
			
		}
		return count;
	}
	
	
	
	/**
	 * 增加一个宠物
	 */
	@Override
	public boolean addPet(PetShop haurun, Pet pet) {
		boolean flag = true;
		
		//得到放置宠物的笼子
		Pet[] pets = haurun.getPets();
		
		int count=0;
		for(int index=0;index<pets.length;index++){
			
			Pet chwu = pets[index];
			
			//如果第一个就是null
			if(chwu == null){
				break;
			}else{
				count++;
				continue;
			}
		}
		if(count<=pets.length-1){
			pets[count] = pet;
		}else{
			flag= false;
			System.out.println("本店已满。恕不接待。。");
		}
		
		return flag;
	}

	
	/**
	 * 根据名字查询宠物
	 */
	@Override
	public Pet findPetByName(PetShop huarun, String name) {
		
		//拿到宠物笼子
		
		Pet[] pets = huarun.getPets();
		
		//遍历数组拿到名字宠物的名字petName == name
		//判断笼子有没有满
		/*for(int index=0;index<pets.length;index++){
			Pet pet = pets[index];
		}*/
 
	    for(Pet pet : pets){
	    	String petName =pet.getName();
	    	
	    	if(petName.equals(name)){
	    		System.out.println("找到了");
	    		return pet;
	    	}
	    }
	
		return null;
	}



	/**根据宠物的年龄查找符合要求的宠物
	 * 
	 */
	@Override
	public Pet[] findPetsByAge(PetShop huarun, int age) {

		Pet[] pets =huarun.getPets();
		int count=0;
		//1. 准备一个多大的笼子来装
		
		Pet[] jieguo = null;
		for(int index=0;index<pets.length;index++){
			//拿到具体的宠物
			Pet pet = pets[index];
			
			//拿到宠物的年龄
			int nl = pet.getAge();
			if(nl==age){
				count++;
			}
			
		}
		
		//结果数组得到
		jieguo = new Pet[count];
		//2.遍历笼子pets  把符合条件的放进去
		count =0;
		for(Pet pet:pets){
			int nl = pet.getAge();
			if(nl==age){
				//找到了  加入新数组
				jieguo[count] = pet;
				count++;
			}
			
		}
		
		
		return jieguo;
	}


	@Override
	public Pet updateByConditions(PetShop huarun, Pet condition) {
		
		String name = condition.getName();
		int age = condition.getAge();
		String color = condition.getColor();
		
		Pet[] pets = huarun.getPets();
		
		for(Pet pet : pets){
			String petName = pet.getName();
			if(name.equals(petName)){
				pet.setAge(age);
				pet.setColor(color);
				return pet;
			}
		}
		
		return null;
	}


	
	

}
